from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from options import options

class Discuz:
    def __init__(self, driver):
        self.driver = driver

    def register(self,username,pwd,email):
        self.driver.find_element(By.CLASS_NAME, 'xi2 xw1').click()
        sleep(1)
        self.driver.find_element(By.ID, 'wIDZp8').send_keys(username)
        sleep(1)
        self.driver.find_element(By.ID, 'vqL88k').send_keys(pwd)
        sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="Iyz22Y"]').send_keys(pwd)
        sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="xOmGZG"]').send_keys(email)
        sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="reginfo_a_btn"]').click()
        sleep(6)
    def logout(self):
        self.driver.find_element(By.XPATH,'//*[@id="um"]/p[1]/a[5]').click()
        sleep(2)
    def login(self):
        self.driver.find_element(By.XPATH, '//*[@id="ls_username"]').send_keys('test01')
        self.driver.find_element(By.XPATH, '//*[@id="ls_password"]').send_keys('123456')
        self.driver.find_element(By.XPATH, '//*[@id="lsform"]/div/div/table/tbody/tr[2]/td[3]/button').click()
        sleep(6)

    def read(self):
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="normalthread_28"]/tr/th/a[2]').click()
        sleep(2)

    def send(self):
        self.driver.find_element(By.XPATH, '//*[@id="category_1"]/table/tbody/tr[1]/td[2]/h2/a').click()
        sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="newspecial"]').click()
        sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="subject"]').send_keys('元旦快乐！')
        # 切换到iframe里面
        self.driver.switch_to.frame('e_iframe')
        self.driver.find_element(By.XPATH, '/html/body').clear()
        self.driver.find_element(By.XPATH, '/html/body').send_keys('元旦快乐元旦快乐元旦快乐元旦快乐元旦快乐')
        sleep(2)
        # dr.switch_to.default_content()  #跳回默认页面
        self.driver.switch_to.parent_frame()
        self.driver.find_element(By.XPATH, '//*[@id="postsubmit"]').click()
        sleep(2)

    def reply(self):
        self.driver.find_element(By.XPATH, '//*[@id="post_reply"]').click()
        # dr.find_element(By.XPATH,'//*[@id="postmessage"]').clear()
        sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="postmessage"]').send_keys('元旦快乐吖！！！')
        sleep(1)
        self.driver.find_element(By.XPATH, '//*[@id="postsubmit"]').click()
        sleep(2)

if __name__ == '__main__':
    dr = webdriver.Chrome(options=options)
    url = r'http://192.168.190.130/upload/'
    dr.get(url)
    dr.maximize_window()
    discuz = Discuz(dr)
    username='lmd333'
    # discuz.register()
    # discuz.logout()
    discuz.login()
    discuz.read()
    # discuz.send()
    discuz.reply()
    dr.quit()
